package com.atguigu.consumer;

import com.atguigu.constant.MqConst;
import com.atguigu.service.SearchIndexService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author ：Hachimen
 * @date ：Created in 2021/9/8 16:49
 * @description：
 * @modified By：
 * @version:
 */
@Component
public class EsConsumer {
    @Autowired
    private SearchIndexService searchIndexService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.ON_SALE_QUEUE,durable = "false"),
            exchange = @Exchange(value = MqConst.ON_OFF_SALE_EXCHANGE,durable = "false"),
            key = {MqConst.ON_SALE_ROUTING_KEY}))
    public void onSale(Long skuId, Message message, Channel channel) throws IOException {
        searchIndexService.onSale(skuId);
        /*
         * 手动签收
         * deliveryTag签收哪个消息
         * multiple 是否应答多个消息 true应答签收多个消息，false只签收当前消息
         */
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.OFF_SALE_QUEUE,durable = "false"),
            exchange = @Exchange(value = MqConst.ON_OFF_SALE_EXCHANGE,durable = "false"),
            key = {MqConst.OFF_SALE_ROUTING_KEY}))
    public void offSale(Long skuId, Message message, Channel channel) throws IOException {
        searchIndexService.offSale(skuId);

        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
